<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\behavior;

use app\common\manager\InviteManager;
use app\common\manager\PlatformManager;
use app\common\manager\StatisticsManager;
use app\common\manager\StoreAccountManager;
use app\common\manager\UserAccountManager;
use app\common\model\Invite;
use app\common\model\Store;
use app\common\model\StoreAccount;
use app\common\model\User;
use app\commons\model\InviteStatistics;
use Monolog\Handler\IFTTTHandler;
use think\Db;
use think\Exception;

class Register
{

    public function run(&$params)
    {
        try {
            $ipRegistered  = false;
            $max  = false;
            $invite = get_addon_config('invite');
            $storeInfo = null;
            if ($params['invited_store_id']){
                $storeInfo = Store::where('id', $params['invited_store_id'])->find();
            }
            if ($invite['filtermode'] == 'advanced') {
                if ($params['invited_user_id']){
                    $ipRegistered = Invite::where('invited_user_id', $params['invited_user_id'])->where('type',Invite::TYPE_USER)->where('auth',Invite::AUTH_YES)->where('ip', $params['joinip'])->find();
                }else if($params['invited_store_id']){
                    $ipRegistered = Invite::where('invited_user_id', $params['invited_store_id'])->where('type',Invite::TYPE_STORE)->where('auth',Invite::AUTH_YES)->where('ip', $params['joinip'])->find();
                }

            }
            $inviteCount  = 0;
            //$type = Invite::TYPE_USER;
            //每日邀请上限
            if ($invite['dailymaxinvite']) {
                if ($params['invited_store_id']){
                    //$type = Invite::TYPE_STORE;
                    $inviteCount = Invite::where('invited_user_id', $params['invited_store_id'])->where('type',Invite::TYPE_STORE)->where('auth',Invite::AUTH_YES)->whereTime('createtime', 'today')->count();
                }else if($params['invited_user_id']){
                    //$type = Invite::TYPE_USER;
                    $inviteCount = Invite::where('invited_user_id', $params['invited_user_id'])->where('type',Invite::TYPE_USER)->where('auth',Invite::AUTH_YES)->whereTime('createtime', 'today')->count();
                }
                if ($inviteCount > $invite['dailymaxinvite']) {
                    $max = true;
                }
            }
            if(!Invite::where('user_id',$params['id'])->where('invited_user_id',$params['invited_user_id'])->update(['auth'=>Invite::AUTH_YES,'update_time'=>time()])){
                throw new Exception('实名信息更新失败');
            }

            if(!User::where('id',$params['invited_store_id'])->setInc('invite_auth_count')){
                throw new Exception('邀请人信息更新失败');
            }
            if (!$ipRegistered && $max===false ) {


                if ($invite['rewardmode']=='basic'){
                    if ($invite['rewardmode_both']=='basic'){
                        if (!empty($invite['rewardscore']) && !empty($params['invited_user_id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['invited_user_id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITE,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }

                        $parent = User::where('id', $params['invited_user_id'])->field('invited_user_id,invited_store_id')->find();
                        //$parent = User::where('id', $params['invited_user_id'])->value('invited_user_id');
                        $invite_user_id = $parent['invited_user_id'];
                        if (!empty($invite['rewardscore']) && !empty($invite_user_id)){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($invite_user_id, $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITE,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }

                    }else if($invite['rewardmode_both']=='backend'){

                        if (!empty($invite['rewardmoney']) && !empty($params['invited_user_id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['invited_user_id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_REWARD, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }

                        $parent = User::where('id', $params['invited_user_id'])->field('invited_user_id,invited_store_id')->find();
                        //$parent = User::where('id', $params['invited_user_id'])->value('invited_user_id');
                        $invite_user_id = $parent['invited_user_id'];
                        $invite_store_id = $parent['invited_store_id'];
                        if (!empty($invite['rewardmoney']) && !empty($invite_user_id)){
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($invite_user_id, $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_REWARD, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }

                        if ($invite['storerewardswitch'] == 'open' && $invite['storerewardmoney'] > 0 && $invite_store_id){
                            $money = bcadd($invite['storerewardmoney'], 0, 2);
                            $storeInfo = Store::where('id', $invite_store_id)->find();
                            $result = StoreAccountManager::money($storeInfo['merchant_id'],$invite_store_id,$money,StoreAccountManager::CHANGE_TYPE_INCOME,StoreAccountManager::TYPE_REWARD,time(),false);
                            if ($result['code'] == 0) {
                                throw new Exception($result['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE_STORE,['recipient'=>$params['invited_store_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }

                            if(!StoreAccount::where('store_id',$params['invited_store_id'])->update([
                                'invite_number'=>Db::raw('invite_number + 1'),
                                'updatetime'=>time(),
                                'version'=>Db::raw('version + 1')
                            ])){
                                throw new Exception('邀请次数更新失败');
                            }

                            $result = StatisticsManager::enter($storeInfo['merchant_id'],$storeInfo['id'],1,time(),InviteStatistics::TIME_TYPE_YEAR,StatisticsManager::TYPE_INVITE,false);
                            if ($result['code'] != StatisticsManager::$successCode){
                                throw new \Exception($result['message']);
                            }
                        }
                    }else if($invite['rewardmode_both']=='all'){

                        if (!empty($invite['rewardscore']) && !empty($params['invited_user_id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['invited_user_id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITE,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }

                        if (!empty($invite['rewardmoney']) && !empty($params['invited_user_id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['invited_user_id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_REWARD, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }

                        $parent = User::where('id', $params['invited_user_id'])->field('invited_user_id,invited_store_id')->find();
                        //$parent = User::where('id', $params['invited_user_id'])->value('invited_user_id');
                        $invite_user_id = $parent['invited_user_id'];
                        $invite_store_id = $parent['invited_store_id'];
                        if (!empty($invite['rewardscore']) && !empty($invite_user_id)){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($invite_user_id, $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITE,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }

                        if (!empty($invite['rewardmoney']) && !empty($invite_user_id)){
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($invite_user_id, $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_REWARD, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }

                        if ($invite['storerewardswitch'] == 'open' && $invite['storerewardmoney'] > 0 && $invite_store_id){
                            $money = bcadd($invite['storerewardmoney'], 0, 2);
                            $storeInfo = Store::where('id', $invite_store_id)->find();
                            $result = StoreAccountManager::money($storeInfo['merchant_id'],$invite_store_id,$money,StoreAccountManager::CHANGE_TYPE_INCOME,StoreAccountManager::TYPE_REWARD,time(),false);
                            if ($result['code'] == 0) {
                                throw new Exception($result['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE_STORE,['recipient'=>$params['invited_store_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }

                            if(!StoreAccount::where('store_id',$params['invited_store_id'])->update([
                                'invite_number'=>Db::raw('invite_number + 1'),
                                'updatetime'=>time(),
                                'version'=>Db::raw('version + 1')
                            ])){
                                throw new Exception('邀请次数更新失败');
                            }

                            $result = StatisticsManager::enter($storeInfo['merchant_id'],$storeInfo['id'],1,time(),InviteStatistics::TIME_TYPE_YEAR,StatisticsManager::TYPE_INVITE,false);
                            if ($result['code'] != StatisticsManager::$successCode){
                                throw new \Exception($result['message']);
                            }
                        }
                    }


                }else if ($invite['rewardmode']=='backend'){
                    if ($invite['rewardmode_both']=='basic'){
                        if (!empty($invite['rewardscore']) && !empty($params['id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITED,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }
                    }else if($invite['rewardmode_both']=='backend'){

                        if (!empty($invite['rewardmoney']) && !empty($params['id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_INVITED, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }
                    }else if($invite['rewardmode_both']=='all'){

                        if (!empty($invite['rewardscore']) && !empty($params['id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITED,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }

                        if (!empty($invite['rewardmoney']) && !empty($params['id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_INVITED, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }
                    }
                }else if ($invite['rewardmode']=='all'){
                    if ($invite['rewardmode_both']=='basic'){
                        if (!empty($invite['rewardscore']) && !empty($params['invited_user_id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['invited_user_id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITE,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }
                    }else if($invite['rewardmode_both']=='backend'){

                        if (!empty($invite['rewardmoney']) && !empty($params['invited_user_id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['invited_user_id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_REWARD, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }
                    }else if($invite['rewardmode_both']=='all'){

                        if (!empty($invite['rewardscore']) && !empty($params['invited_user_id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['invited_user_id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITE,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }

                        if (!empty($invite['rewardmoney']) && !empty($params['invited_user_id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['invited_user_id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_REWARD, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }
                    }


                    if ($invite['rewardmode_both']=='basic'){
                        if (!empty($invite['rewardscore']) && !empty($params['id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITED,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }
                    }else if($invite['rewardmode_both']=='backend'){

                        if (!empty($invite['rewardmoney']) && !empty($params['id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_INVITED, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }
                        }
                    }else if($invite['rewardmode_both']=='all'){

                        if (!empty($invite['rewardscore']) && !empty($params['id'])){

                            $score = bcadd($invite['rewardscore'], 0,2);
                            $resultInvited = UserAccountManager::score($params['id'], $score,UserAccountManager::CHANGE_TYPE_INCOME,time(),UserAccountManager::TYPE_SCORE_INVITED,[],false);
                            if ($resultInvited['code'] == 0){
                                throw new Exception($resultInvited['message']);
                            }

                        }

                        if (!empty($invite['rewardmoney']) && !empty($params['id'])) {
                            $money = bcadd($invite['rewardmoney'], 0, 2);
                            $resultInvited = UserAccountManager::money($params['id'], $money, UserAccountManager::CHANGE_TYPE_INCOME, time(), UserAccountManager::TYPE_INVITED, ['relation_id'=>$params['id']], false);
                            if ($resultInvited['code'] == 0) {
                                throw new Exception($resultInvited['message']);
                            }

                            $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE,['recipient'=>$params['invited_user_id'],'relation_id'=>$params['id']],false);
                            if ($resultPlatform['code'] == 0) {
                                throw new Exception($resultPlatform['message']);
                            }
                        }
                    }
                }

                if ($invite['storerewardswitch'] == 'open' && $invite['storerewardmoney'] > 0 && $params['invited_store_id']){
                    $money = bcadd($invite['storerewardmoney'], 0, 2);
                    $result = StoreAccountManager::money($storeInfo['merchant_id'],$params['invited_store_id'],$money,StoreAccountManager::CHANGE_TYPE_INCOME,StoreAccountManager::TYPE_REWARD,time(),false);
                    if ($result['code'] == 0) {
                        throw new Exception($result['message']);
                    }

                    $resultPlatform = PlatformManager::platformLog($money,time(),\app\common\model\Platform\Log::DIRECTIVE_OUT,\app\common\model\Platform\Log::TYPE_INVITE_STORE,['recipient'=>$params['invited_store_id'],'relation_id'=>$params['id']],false);
                    if ($resultPlatform['code'] == 0) {
                        throw new Exception($resultPlatform['message']);
                    }

                    if(!StoreAccount::where('store_id',$params['invited_store_id'])->update([
                        'invite_number'=>Db::raw('invite_number + 1'),
                        'updatetime'=>time(),
                        'version'=>Db::raw('version + 1')
                    ])){
                        throw new Exception('邀请次数更新失败');
                    }

                    $result = StatisticsManager::enter($storeInfo['merchant_id'],$storeInfo['id'],1,time(),InviteStatistics::TIME_TYPE_YEAR,StatisticsManager::TYPE_INVITE,false);
                    if ($result['code'] != StatisticsManager::$successCode){
                        throw new \Exception($result['message']);
                    }
                }
            }


        }catch (Exception $exception){
            file_put_contents('invite.txt',$exception->getMessage().$exception->getLine().PHP_EOL,FILE_APPEND);
        }

    }

}
